package com.example.layoutdesign.DataSource;

import java.util.ArrayList;
import java.util.List;

import com.example.layoutdesign.Model.ChatHistory;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;


public class ChatHistoryDataSource extends DataSourceBase  {

	
	 public final String KEY_CHATSESSIONID = "chathistoryID";
	 public final String KEY_TEXT = "text";
	 public final String MYDATABASE_TABLE = "chathistory";
	 
	 private String SCRIPT_CREATE_DATABASE_TABLE =
	  "create table " + MYDATABASE_TABLE + " ("
	  + KEY_CHATSESSIONID + " integer not null," + KEY_TEXT + " text null);";
	 
	 public ChatHistoryDataSource(Context c){
		 super(c);
	 }
	 
	 public String getCreateTableQuery() {
		return this.SCRIPT_CREATE_DATABASE_TABLE;
	};
	  
	@Override
	public String getTableName() {
		return this.MYDATABASE_TABLE;
	}
	
	public long insert(ChatHistory c){
	  
	  ContentValues contentValues = new ContentValues();
	  contentValues.put(KEY_CHATSESSIONID, c.getChatHistoryID());
	  contentValues.put(KEY_TEXT, c.getText());
	  r
	  return sqLiteDatabase.insert(MYDATABASE_TABLE, null, contentValues);
	 }
	 
	public List<ChatHistory> getAll(){
		 
		 List<ChatHistory> historyRecords = new ArrayList<ChatHistory>();
		 String[] columns = new String[]{KEY_CHATSESSIONID, KEY_TEXT};
		 Cursor cursor = sqLiteDatabase.query(MYDATABASE_TABLE, columns,null, null, null, null, null);
	
		 for(cursor.moveToFirst(); !(cursor.isAfterLast()); cursor.moveToNext()){
			 ChatHistory history = this.cursorToChatHistory(cursor);
			 historyRecords.add(history);
		 }
		 return historyRecords;
	 }
 
 
	private ChatHistory cursorToChatHistory(Cursor cursor) {
		 ChatHistory history = new ChatHistory();
		    history.setChatHistoryID(cursor.getLong(0));
		    history.setText(cursor.getString(1));
		    history.setDateTime(cursor.getString(2));
		    return history;
	}
}
